<template>
  <div>
    <h1>修改密码</h1>
    <hr />
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
   
      <el-row :gutter="20">
        <el-col :span="10"
          ><el-form-item label="输入新密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input> </el-form-item
        ></el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10"
          ><el-form-item label="确认密码" prop="checkPasstwo">
            <el-input
              type="password"
              v-model="ruleForm.checkPasstwo"
              autocomplete="off"
            ></el-input> </el-form-item
        ></el-col>
      </el-row>
      <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm');"
            >立即修改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
           </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    var checkPass=(rlue,value,callback)=>{
           if (!value) {
          return callback(new Error('新密码不能为空'));
        }
    };
    var checkPasstwo=(rlue,value,callback)=>{
      if (!value) {
          return callback(new Error('确认密码不能为空'));
        }
        else if(value !== this.ruleForm.checkPass){
           callback(new Error('两次输入密码不一致!'));
        }else{
           callback();
        }

    }
    return {
      ruleForm: {
        checkPass: '',
        checkPasstwo: '',
      },
      rules:{
         
          checkPass: [
            { validator: checkPass, trigger: 'blur' }
          ],
          checkPasstwo: [
            { validator: checkPasstwo, trigger: 'blur' }
          ],
      }
    }
  },
  methods:{
     resetForm(formName) {
        this.$refs[formName].resetFields();
      },
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
            this.test()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    test(){ 
      let data=new Object()
      let name=this.$store.state.user.username
      console.log(name)
      data.password=this.password
      data.id=this.$store.state.user.id
      if(name=="wudaye"){
         this.$axios.post('/admin/editPass',data)
      .then(res=>{
        console.log(res)
      })
      .catch(err=>{
        console.log(err)
      })
      }else{
         this.$axios.post('/user/editPassword',data)
      .then(res=>{
        console.log(res)
      })
      .catch(err=>{
        console.log(err)
      })
      }
     
    }
  }
}
</script>